package com.lrkj.notice.controller;

import com.lrkj.common.annotation.Log;
import com.lrkj.common.core.controller.BaseController;
import com.lrkj.common.core.domain.AjaxResult;
import com.lrkj.common.core.page.TableDataInfo;
import com.lrkj.common.enums.BusinessType;
import com.lrkj.common.enums.NoticeType;
import com.lrkj.notice.domain.NoticeSiteMessage;
import com.lrkj.notice.domain.NoticeSiteMessageUser;
import com.lrkj.notice.service.INoticeSiteMessageService;
import com.lrkj.notice.service.INoticeSiteMessageUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 站内消息Controller
 *
 * @author yajun_ren
 * @date 2022-09-07
 */
@RestController
@RequestMapping("/notice/noticeSiteMessage")
@Api(tags = "站内消息")
public class NoticeSiteMessageController extends BaseController {
    @Autowired
    private INoticeSiteMessageService noticeSiteMessageService;

    @Autowired
    private INoticeSiteMessageUserService noticeSiteMessageUserService;

    /**
     * 查询站内消息列表
     */
    @ApiOperation(value = "查询站内消息列表", notes = "查询站内消息列表")
    @PreAuthorize("@ss.hasPermi('notice:noticeSiteMessage:list')")
    @GetMapping("/list")
    public TableDataInfo list(NoticeSiteMessage noticeSiteMessage) {
        startPage();
        this.changeParameter(noticeSiteMessage.getParams());
        List<NoticeSiteMessage> list = noticeSiteMessageService.selectNoticeSiteMessageList(noticeSiteMessage);
        return getDataTable(list);
    }

    /**
     * 获取站内消息详细信息
     */
    @ApiOperation(value = "获取站内消息详细信息", notes = "获取站内消息详细信息")
    @PreAuthorize("@ss.hasPermi('notice:noticeSiteMessage:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return AjaxResult.success(noticeSiteMessageService.selectNoticeSiteMessageById(id));
    }

    /**
     * 新增站内消息
     */
    @ApiOperation(value = "新增站内消息", notes = "新增站内消息")
    @PreAuthorize("@ss.hasPermi('notice:noticeSiteMessage:add')")
    @Log(title = "站内消息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody NoticeSiteMessage noticeSiteMessage) {
        return toAjax(noticeSiteMessageService.insertNoticeSiteMessage(noticeSiteMessage));
    }

    /**
     * 修改站内消息
     */
    @ApiOperation(value = "修改站内消息", notes = "修改站内消息")
    @PreAuthorize("@ss.hasPermi('notice:noticeSiteMessage:edit')")
    @Log(title = "站内消息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody NoticeSiteMessage noticeSiteMessage) {
        return toAjax(noticeSiteMessageService.updateNoticeSiteMessage(noticeSiteMessage));
    }

    /**
     * 删除站内消息
     */
    @ApiOperation(value = "删除站内消息", notes = "删除站内消息")
    @PreAuthorize("@ss.hasPermi('notice:noticeSiteMessage:remove')")
    @Log(title = "站内消息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(noticeSiteMessageService.deleteNoticeSiteMessageByIds(ids));
    }

    /**
     * 消息发布
     */
    @ApiOperation(value = "站内消息发布", notes = "站内消息发布")
    @Log(title = "站内消息发布", businessType = BusinessType.UPDATE)
    @GetMapping("/msgPublish/{ids}")
    public AjaxResult msgPublish(@PathVariable String[] ids) {
        return toAjax(noticeSiteMessageService.msgPublish(ids, NoticeType.SEND_STATE_20.getCode()));
    }

    /**
     * 站内消息撤回
     */
    @ApiOperation(value = "站内消息撤回", notes = "站内消息撤回")
    @Log(title = "站内消息撤回", businessType = BusinessType.UPDATE)
    @GetMapping("/msgRecall/{ids}")
    public AjaxResult msgRecall(@PathVariable String[] ids) {
        return toAjax(noticeSiteMessageService.msgPublish(ids, NoticeType.SEND_STATE_30.getCode()));
    }


    /**
     * 我的消息列表
     */
    @ApiOperation(value = "我的消息列表", notes = "我的消息列表")
    @GetMapping("/myNoticeList")
    public TableDataInfo myNoticeList(NoticeSiteMessageUser messageUser) {
        startPage();
        List<NoticeSiteMessageUser> list = noticeSiteMessageUserService.selectNoticeSiteMessageUserList(messageUser);
        return getDataTable(list);
    }


    /**
     * 更新未读状态
     */
    @ApiOperation(value = "更新未读状态", notes = "更新未读状态")
    @Log(title = "更新未读状态", businessType = BusinessType.UPDATE)
    @GetMapping("/updateHaveRead/{id}")
    public AjaxResult updateHaveRead(@PathVariable String id) {
        return toAjax(noticeSiteMessageUserService.updateHaveRead(id));
    }

    /**
     * 消息未读条数
     */
    @ApiOperation(value = "消息未读条数", notes = "消息未读条数")
    @Log(title = "消息未读条数", businessType = BusinessType.UPDATE)
    @GetMapping("/notReadMsgCountRead/{receiveId}")
    public AjaxResult notReadMsgCountRead(@PathVariable String receiveId) {
        return AjaxResult.success(noticeSiteMessageUserService.notReadMsgCountRead(receiveId));
    }

    /**
     * 全部更新为已读
     */
    @ApiOperation(value = "全部更新为已读", notes = "全部更新为已读")
    @Log(title = "全部更新为已读", businessType = BusinessType.UPDATE)
    @GetMapping("/updateNoRead/{receiveId}")
    public AjaxResult updateNoRead(@PathVariable String receiveId) {
        return AjaxResult.success(noticeSiteMessageUserService.updateNoRead(receiveId));
    }



    /**
     * 小程序我的通知列表
     */
    @ApiOperation(value = "小程序我的通知列表", notes = "小程序我的通知列表")
    @GetMapping("/noticeGroupList/{userName}")
    public AjaxResult noticeGroupList(@PathVariable String userName) {
        return AjaxResult.success(noticeSiteMessageUserService.noticeGroupList(userName));
    }


    /**
     * 获取用户消息详细信息
     */
    @ApiOperation(value = "获取用户消息详细信息", notes = "获取用户消息详细信息")
    @GetMapping(value = "/userMsgById/{id}")
    public AjaxResult userMsgById(@PathVariable("id") String id) {
        return AjaxResult.success(noticeSiteMessageUserService.selectNoticeSiteMessageUserById(id));
    }

}
